<template>
  <div class="login-page">
    <van-nav-bar
  title="面经登录"
/>
<van-form @submit="onSubmit">
  <van-field
    v-model="user.username"

    label="用户名"
    placeholder="用户名"
   :rules="rules.username"
  />
  <van-field
    v-model="user.password"
    type="password"

    label="密码"
    placeholder="密码"
    :rules="rules.password"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
<router-link to="/register" class="link">注册账号</router-link>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      user: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '账号不得小于5' }
        ],
        password: [
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '密码不得小于6' }
        ]
      }
    }
  },
  methods: {
    async onSubmit() {
      try {
        const res = await loginAPI(this.user)
        this.$toast('登录成功')
        setToken(res.data.token)
        this.$router.push('/')
      } catch {}
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
